<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.sql.*" %>
<html>
<head>
    <title>所有订单</title>
</head>
<body>
<%
    Connection conn = null;
    Statement stmt = null;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false",
                "root", "123456");
        stmt = conn.createStatement();

        if(conn != null){
            out.print("数据库连接成功！<br />");
%>
<%
    String message = (String) request.getAttribute("message");
    if (message != null) {
%>
<p><%= message %></p>
<%
    }
%>
<table border="2">
    <tr>
        <td>订单编号</td>
        <td>商品名称</td>
        <td>商品编号</td>
        <td>单价</td>
        <td>数量</td>
        <td>是否收货</td>
        <td>操作</td>
    </tr>
    <%
        ResultSet rs = stmt.executeQuery("SELECT * FROM order_details");
        while (rs.next()) {
            String isDelivered = rs.getInt("is_delivered") == 0 ? "未收货" : "已收货";
            String orderId = rs.getString("order_id");
    %>
    <tr>
        <td><%= rs.getString("order_id") %></td>
        <td><%= rs.getString("product_name") %></td>
        <td><%= rs.getString("product_code") %></td>
        <td><%= rs.getString("unit_price") %></td>
        <td><%= rs.getInt("quantity") %></td>
        <td><%= isDelivered %></td>
        <td>
            <% if (rs.getInt("is_delivered") == 0) { %>
            <form action="ConfirmDeliveryServlet" method="post" class="confirm-delivery-form">
                <input type="hidden" name="orderId" value="<%= orderId %>">
                <button type="submit" class="confirm-delivery-btn">确认收货</button>
            </form>
            <% } %>
        </td>
    </tr>
    <%
            }
        } else {
            out.print("连接失败！");
        }
    %>
</table>
<%
    } catch (Exception e) {
        out.print("数据库连接异常！");
    } finally {
        try {
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
%>
</body>
</html>